#include "addwidget.h"
#include "ui_addwidget.h"
#include<QErrorMessage>
Addwidget::Addwidget(Graph *t ,QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Addwidget)
{
    tmp =t;
    ui->setupUi(this);
    this->setWindowTitle("添加交通记录");
    ui->comboBox_which->addItem(" ");
    ui->comboBox_which->addItem("列车");
    ui->comboBox_which->addItem("飞机");
}

Addwidget::~Addwidget()
{
    delete ui;
}

void Addwidget::on_pushButton_OK_clicked()
{
    if(ui->lineEdit_BGC->text().isEmpty()){
        QErrorMessage *dlg=new QErrorMessage(this);
        dlg->setWindowTitle("错误信息");
        dlg->showMessage("请填写起始城市！");
        return;
    }
    else if(ui->lineEdit_LTC->text().isEmpty()){
        QErrorMessage *dlg=new QErrorMessage(this);
        dlg->setWindowTitle("错误信息");
        dlg->showMessage("请填写终点城市！");
        return;
    }
    else if(ui->lineEdit_BGT->text().isEmpty()){
        QErrorMessage *dlg=new QErrorMessage(this);
        dlg->setWindowTitle("错误信息");
        dlg->showMessage("请填写起始时间！");
        return;
    }
    else if(ui->lineEdit_LTT->text().isEmpty()){
        QErrorMessage *dlg=new QErrorMessage(this);
        dlg->setWindowTitle("错误信息");
        dlg->showMessage("请填写终止时间！");
        return;
    }
    else if(ui->lineEdit_cost->text().isEmpty()){
        QErrorMessage *dlg=new QErrorMessage(this);
        dlg->setWindowTitle("错误信息");
        dlg->showMessage("请填写票价！");
        return;
    }
    else if(ui->comboBox_which->currentIndex()==0){
        QErrorMessage *dlg=new QErrorMessage(this);
        dlg->setWindowTitle("错误信息");
        dlg->showMessage("请填写交通方式！");
        return;
    }
    else {
        date* b1=new date(ui->lineEdit_BGT->text());
        date* l1=new date(ui->lineEdit_LTT->text());
        bool added=tmp->addtfinfo(ui->lineEdit_BGC->text(),ui->lineEdit_LTC->text(),*b1,*l1,ui->lineEdit_cost->text().toInt(),ui->comboBox_which->currentIndex()-1);
        if(added){
            this->close();
        }
        else{
            QErrorMessage *dlg=new QErrorMessage(this);
            dlg->setWindowTitle("错误信息");
            dlg->showMessage("请检查城市信息是否有误！");
            return;
        }
    }
}

void Addwidget::on_pushButton_cancel_clicked()
{
    this->close();
}
